package com.cqupt.slewing

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation
import kotlinx.android.synthetic.main.fragment_set.*
import kotlinx.coroutines.launch

class SetFragment : Fragment() {
    private lateinit var myViewModel: MyViewModel
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_set, container, false)
    }

    override fun onStart() {
        super.onStart()
        myViewModel = ViewModelProvider(requireActivity())[MyViewModel::class.java]

        buttonLogout.setOnClickListener {
            lifecycleScope.launch {
                myViewModel.userLogout()
                Intent(requireActivity(), MyService::class.java).also {
                    requireActivity().stopService(it)
                }
                Navigation.findNavController(it).also {
                    it.navigate(R.id.action_setFragment_to_welcomeActivity)
                }
            }
        }
    }

}